import storage from '@/libs/storage';
// 埋点
class BuryPoint {
  install(Vue) {
    const $vue = new Vue();
    Vue.prototype.$buryPoint = {
      init: async (params) => {
        await $vue.$api.buryPoint(params, { showLoading: false });
      },
      /**
       * 
       * @param {eventValue, eventName, page, startTime, endTime, orderId, productIds } params
       * @param {*} callback 
       */
      action: function (params = {}) {
        const { eventName, page, startTime, endTime, orderId, productIds } = params;
        // 触发af埋点
        const afPointList = ['payInfo_submit_success']
        const needAfPoint = afPointList.includes(eventName)
        const appVersion = storage.get('appVersion') || '1.0.0';
        if (appVersion < '1.0.5' && needAfPoint) {
          $vue.$hybrid.action('ee', {
            content: eventName,
            value: {
              productId: productIds,
            },
          })
          return
        }
        // 埋点名： eventName
        // 埋点值： eventValue
        // 所在页面：page
        // installTime 安装时间
        // startTime 进入页面时间
        // endTime 离开页面时间
        // orderId 订单号
        const user = storage.get('user') || {}
        const deviceId = storage.get('deviceId') || null
        const crossUser = storage.get('crossUser') || null
        const afid = storage.get('afid') || null
        const installTime = storage.get('installTime') || null
        const marketChannel = storage.get('marketChannel') || 'googlePlay';
        const reqParams = {
          petoEvnameSharePart: eventName, // 埋点名 eventName
          petoEvtimeSharePart: new Date().getTime(), // 埋点时间 eventTime
          // ---------------- 埋点H5新增属性 ---------------- 
          petoStartTimeSharePart: startTime || '',
          petoEndTimeSharePart: endTime || '',
          petoUseIdSharePart: user.userId || '', // userId
          petoPhoneSharePart: user.phone || '', // phone
          petoMediaSourceSharePart: marketChannel, // mediaSource
          petoDeviceIComPter: deviceId, // deviceId
          petoafIdharePart: afid, // afId
          petoPageSharePart: page || '', // page
          petoCrossUserSharePart: crossUser, // crossUser
          petoInstallTimeComPter: installTime || null, // installTime
          // --------------------------混淆前 ------------------------
          // eventName: eventName, // 埋点名 eventName
          // eventValue: params.eventValue || {}, // 埋点值 eventValue
          // eventTime: new Date().getTime(), // 埋点时间 eventTime
          // // ---------------- 埋点H5新增属性 ---------------- 
          // userId: user.userId || '', // userId
          // phone: user.phone || '', // phone
          // mediaSource: '1111', // mediaSource
          // deviceId: deviceId, // deviceId
          // afId: '', // afId
          // page: '', // page
          // crossUser: 0, // crossUser
          // installTime: null, // crossUser
        };
        // 埋点值 eventValue --- 没有值不传
        if (params.eventValue) reqParams.petoEvalueSharePart = params.eventValue
        // 上传AF埋点---andriod
        const afEventValue = {}
        if (orderId && needAfPoint) afEventValue.order_id = orderId
        $vue.$hybrid.action('afPoint', {
          eventName,
          eventValue: afEventValue
        })
        this.init(reqParams);
      },
    };
  }
}

export default new BuryPoint();
